<?php

namespace app\admin\model;

use think\Model;

class QuestionsAnswers extends Model
{
    protected $autoWriteTimestamp = true;

    public static function getList($questionId)
    {
        $where = [];
        $where[] = ['qid', '=', $questionId];
        $limit = input('get.limit');
        if ($search = input('get.name')) {
            $where[] = ['a.title', 'like', "%" . $search . "%"];
        }
        if (input('get.status')) $where[] = ['a.status', '=', input('get.status') - 1];
        $list = self::order('id', 'asc')
            ->where($where)
            ->paginate($limit);
        foreach ($list as $k => $v) {
            $list[$k]['key'] = chr(65 + $k);
        }
        return ['code' => 0, 'data' => $list->items(), 'extend' => ['count' => $list->total(), 'limit' => $limit]];
    }

    public static function createJudgeOption($qid)
    {
        return self::insertAll([
            [
                'qid' => $qid,
                'title' => '正确',
                'create_time' => time(),
                'update_time' => time()
            ],
            [
                'qid' => $qid,
                'title' => '错误',
                'create_time' => time(),
                'update_time' => time()
            ],
        ]);
    }
}